import re

import requests
from PIL import Image
from lxml import etree
# from renting import tools
from pymongo import MongoClient

from chaojiying_Python import Chaojiying_Client

list = []


class Ziru(object):
    def __init__(self):
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
        }
        cookies = "CURRENT_CITY_CODE=510100; _csrf=T3yZZOH6y7aZWfKf2nEOiD2jAn4pFu-x; sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221764a91622915-0013e0449bbc5c-930346c-1327104-1764a91622a2c8%22%2C%22%24device_id%22%3A%221764a91622915-0013e0449bbc5c-930346c-1327104-1764a91622a2c8%22%2C%22props%22%3A%7B%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; gr_user_id=15ca6e9b-f957-4e96-839f-086f3f33c3b1; gr_session_id_8da2730aaedd7628=37517c08-e064-46f0-9f07-ad4d6df6c04d; gr_session_id_8da2730aaedd7628_37517c08-e064-46f0-9f07-ad4d6df6c04d=true; Hm_lvt_4f083817a81bcb8eed537963fc1bbf10=1607568812; Hm_lpvt_4f083817a81bcb8eed537963fc1bbf10=1607569080"
        self.cookies_dict = {temp[:temp.find("=")]: temp[temp.find("=") + 1:] for temp in cookies.split("; ")}

    def main(self, url):
        self.r = requests.post(url=url, headers=self.headers, cookies=self.cookies_dict)
        #
        self.html = etree.HTML(self.r.text)
        # print(self.r.text)
        info_list = self.html.xpath('//div[@class="Z_list-box"]/div')
        img_list = self.html.xpath('/html/body/section/div[3]/div[2]/div[1]/div[2]/div[2]/span[2]/@style')
        img = img_list[0]
        if img:
            ret = re.search(r".*\((.*?)\)", img)
            url = ret.group(1)
            url = "http:" + url
            img_content = requests.get(url=url, headers=self.headers)
            with open("./int_image.png", "wb") as f:
                f.write(img_content.content)

        im = Image.open('./int_image.png')
        x, y = im.size
        try:
            p = Image.new('RGBA', im.size, (128, 128, 128))
            p.paste(im, (0, 0, x, y), im)
            p.save('./shuzi.png')
        except:
            pass
        chaojiying = Chaojiying_Client('yueyue315', 'yueyue.3.15', '96001')
        im = open('./shuzi.png', 'rb').read()
        img_dict = chaojiying.PostPic(im, 1010)
        img_code = img_dict.get("pic_str")
        # print(img_code)
        print("提取到的个数：", len(info_list))

        for info in info_list:
            item = {}
            try:
                item['title'] = info.xpath('.//h5/a/text()')[0]
            except:
                continue
            lou = info.xpath('.//div[@class="desc"]/div/text()')[0]
            lou_index = lou.rfind('|')
            # print(lou_index)
            lou_text = lou[lou_index + 2:]
            lou_str = lou_text.rfind('/')
            item["floor"] = lou_text[:lou_str]
            item["all_floor"] = lou_text[lou_str + 1:-1]

            loaction = info.xpath('.//div[@class="location"]/text()')
            loaction_text = loaction[0].strip()
            item['loaction'] = loaction_text
            list = []
            for i in img_code:
                list.append(i)
                # print(list)
            div_list = self.html.xpath('//div[@class="Z_list-box"]/div')
            for one_div in div_list:
                float_int = one_div.xpath('./div[2]/div[2]/span/@style')
                if float_int:
                    price = ""
                    for i in float_int:
                        index = i.rfind(':')
                        int_ = i[index + 1:-2]
                        price_index = float(int_) / 21.4
                        # print(price_index)
                        price += list[int(price_index)]
                        item['price'] = price
            print(item)
            client = MongoClient("127.0.0.1", 27017)
            collection = client["test"]["test"]
            collection.insert_one(item)


if __name__ == '__main__':
    zr = Ziru()
    for i in range(1, 6):
        url = 'http://cd.ziroom.com/z/p%s/' % (i)
        zr.main(url)
